package com.dmgis.qxfw.dataservice.businessservice.yydl.service.Impl;


import com.dmgis.entity.DmMapPoint;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.msgmediumsmallscale;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.township_station;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.ybzz_forecastfinal;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.TownShipStation;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.MsgMedium;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.yydl.service.HourTempServiceI;
import com.dmgis.qxfw.dataservice.gisservice.service.ITransformService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
public class HourTempServiceImpl implements HourTempServiceI {

	ProcessRequestController pc = new ProcessRequestController();

	@Resource
	private ITransformService transformService;
	@Override
	public List<String> loadCountyPower() {
		List<township_station> listData=new ArrayList<township_station>();
		List<String> resultList=new ArrayList<String>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("StationName");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		strWhere.append(
				String.format(" and city = '"+DbConfig.COUNTY+"'"));
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "township_station");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<township_station>>() {
			});
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		if (listData!=null&&listData.size()>0) {
			for (township_station msg : listData) {
				String smallscale=new String();
				smallscale = msg.getStationname();
				resultList.add(smallscale);
			}
		}

		return resultList;
	}

	@Override
	public List<township_station> getStationId(String n,String j) {
		List<township_station> listData=new ArrayList<township_station>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		strWhere.append(" stationid='"+n+"' or stationid='"+j+"' or stationid ='"+DbConfig.STATIONID+"' ");
		selectRequest.setParam(strWhere.toString());
		String message = "";
		try {
			selectParam.put("method", "township_station");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<township_station>>() {
			});
		} catch (Exception e) {
			System.err.println("查询id异常:" + message);
			e.printStackTrace();
		}
		return listData;
	}

	@Override
	public List<TownShipStation> getTownShipStation(String s1, String s2, String s3) {
		List<ybzz_forecastfinal> listData=new ArrayList<ybzz_forecastfinal>();
		List<township_station> listData1=new ArrayList<township_station>();
		List<TownShipStation> resultList=new ArrayList<TownShipStation>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("stationid");
		columns.add("(cast(datechar as timestamp)+ cast(ntimes||'hour' as INTERVAL)) as datechar");
		columns.add("CAST(cast(temperature AS VARCHAR) as numeric(18,1)) as temperature");
		columns.add("CAST(cast(humid AS VARCHAR) as numeric(18,1)) as humid");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		if (s1!=null) {
			strWhere.append(String.format("(cast(datechar as timestamp)+ cast(ntimes||'hour' as INTERVAL))>='%s'",s1));
		}
		if (s2!=null) {
			strWhere.append(String.format(" and (cast(datechar as timestamp)+ cast(ntimes||'hour' as INTERVAL))<='%s'",s2));
		}
		if (s3!=null) {
			strWhere.append(String.format(" and stationid='%s'",s3));
		}
		strWhere.append(String.format(" and forecasttype='%s'","1"));
		selectRequest.setParam(strWhere.toString());
		selectRequest.setSort(new String[]{"(cast(datechar as timestamp)+ cast(ntimes||'hour' as INTERVAL)) asc"});
		try {
			selectParam.put("method", "ybzz_forecastfinal");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<ybzz_forecastfinal>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData!=null&&listData.size()>0) {
			for (ybzz_forecastfinal msg : listData) {
				TownShipStation smallscale=new TownShipStation();
				smallscale.setDateTime(msg.getDatechar());
				smallscale.setTemperature(msg.getTemperature());
				smallscale.setHumid(msg.getHumid());
				Map<String, String> selectParam1 = new HashMap<String, String>();
				RequestDataBySelect selectRequest1 = new RequestDataBySelect();
				List<String> columns1 = new ArrayList<String>();
				columns1.add("stationid");
				columns1.add("stationname");
				columns1.add("longitude");
				columns1.add("latitude");
				selectRequest1.setColumns(columns1.toArray(new String[columns1.size()]));
				// 拼接where字段
				StringBuilder strWhere1 = new StringBuilder();
				int whereCount1 = 0;
				if (msg.getStationid()!=null) {
					strWhere1.append(String.format(" and stationid='%s'",msg.getStationid()));
				}
				if (whereCount1 == 0 && !StringUtil.IsNullOrEmpty(strWhere1.toString())) {
					strWhere1 = strWhere1.replace(0, 4, "");
				}
				selectRequest1.setParam(strWhere1.toString());
				try {
					selectParam1.put("method", "township_station");
					selectParam1.put("sqlType", "select");
					selectParam1.put("param", JsonUtil.object2Json(selectRequest1));
					String message = pc.processRequest(selectParam1);
					listData1 = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<township_station>>() {
					});
				} catch (Exception e) {
					// TODO: handle exception
				}
				if (listData1!=null&&listData1.size()>0) {
					for (township_station station : listData1) {
						smallscale.setStationID(station.getStationid());
						smallscale.setStationName(station.getStationname());
						smallscale.setLongitude(station.getLongitude().toString());
						smallscale.setLatitude(station.getLatitude().toString());
						DmMapPoint dmDot = new DmMapPoint(station.getLongitude(), station.getLatitude());
						DmMapPoint ltDot = transformService.LonLatConvertToLambert(dmDot.x, dmDot.y);
						smallscale.setY(ltDot.y);
						smallscale.setX(ltDot.x);
					}
				}
				resultList.add(smallscale);
			}
		}
		return resultList;

	}

	@Override
	public List<MsgMedium> getMsgMediumSmallScaleTemp(String s1, String s2, String s3) {
		List<msgmediumsmallscale> listData=new ArrayList<msgmediumsmallscale>();
		List<MsgMedium> resultList=new ArrayList<MsgMedium>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(observTime, 'yyyy-MM-dd HH24:MI:SS') as observTime");
		columns.add("CAST(replace(cast(DryBulbTemp AS VARCHAR),'-65535','0') as numeric(18,1)) as dryBulbTemp");
		columns.add("relhumidity");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (s1!=null) {
			strWhere.append(String.format(" and stationid='%s'",s1));
		}
		if (s2!=null) {
			strWhere.append(String.format(" and observTime>='%s'", s2));
		}
		if (s3!=null) {
			strWhere.append(String.format(" and observTime<='%s'", s3));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		selectRequest.setSort(new String[]{"observTime asc"});
		try {
			selectParam.put("method", "msgmediumsmallscale");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<msgmediumsmallscale>>() {
			});
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		if (listData!=null&&listData.size()>0) {
			for (msgmediumsmallscale msg : listData) {
				MsgMedium smallscale=new MsgMedium();
				smallscale.setObservTime(msg.getObservtime());
				smallscale.setStationID(msg.getStationid());
				smallscale.setDryBulbTemp(msg.getDrybulbtemp().toString());
				smallscale.setRelHumidity(msg.getRelhumidity().toString());
				resultList.add(smallscale);
			}
		}
		return resultList;
	}




	@Override
	public Date liveInitTime() {
		// TODO Auto-generated method stub
		Date date = new Date();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime" };
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "observtime desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "msgmediumsmallscale");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<msgmediumsmallscale>>() {
			}).get(0).getObservtime();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}
}
